package com.doge.controller.admin;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.base.BaseRespVo;
import com.doge.bean.pojo.role.Role;
import com.doge.bean.vo.role.PermReqResult;
import com.doge.bean.vo.role.PermissionVO;
import com.doge.bean.vo.role.PostPermissionBO;
import com.doge.service.admin.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: doge
 * @description: 角色管理
 * @author: Keyu Li
 * @create: 2021-11-29 20:50
 **/


@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam param, String name){
        BaseData data = roleService.list(param, name);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("options")
    public BaseRespVo options(){
        // 查询 role表 获得 BaseData
        BaseData data =  roleService.options();
        return BaseRespVo.ok(data);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Role role) throws Exception {
        roleService.create(role);
        return BaseRespVo.ok(role);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Role role) throws Exception {
        roleService.update(role);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Role role) throws Exception {
        roleService.delete(role);
        return BaseRespVo.ok();
    }

    @GetMapping("permissions")
    public BaseRespVo permissionsGet(String roleId){
        // 返回所有 permission
        PermissionVO vo =roleService.getPersmissions(Integer.parseInt(roleId));
        return BaseRespVo.ok(vo);
    }

    @PostMapping("permissions")
    public BaseRespVo permissionsPost(@RequestBody PostPermissionBO bo){
        roleService.postPermissions(bo.getRoleId(),bo.getPermissions());
        return BaseRespVo.ok();
    }

    /*
    * 临时使用，向 PermissionList 中添加数据
    * */
    @PostMapping("insert/permission")
    public BaseRespVo insertPermissions(@RequestBody PermReqResult base){
        roleService.insertPermissions(base.getData());
        return BaseRespVo.ok();
    }
}
